﻿using System;
using COM.DTO;
using NHibernate;
using COM.Logging;
using NHibernate.Criterion;

namespace DAL.DAO
{
    class ContactTypeDAO
    {
        public static void Add(ContactType contactType)
        {
            {
                using (ISession session = NHibernateHelper.OpenSession())
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.SaveOrUpdate(contactType);
                    transaction.Commit();
                }
            }
        }
        public static void Update(ContactType contactType)
        {
            {
                using (ISession session = NHibernateHelper.OpenSession())
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Update(contactType);
                    transaction.Commit();
                }
            }
        }

        public static void Remove(ContactType contactType)
        {
            {
                using (ISession session = NHibernateHelper.OpenSession())
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Delete(contactType);
                    transaction.Commit();
                }
            }
        }

        public static ContactType GetContactTypeById(String contactTypeId)
        {
            using (ISession session = NHibernateHelper.OpenSession())
                return session.Get<ContactType>(contactTypeId);
        }
        public static ContactType GetUserByUsername(string contactTypeName)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                var contactType = session
                    .CreateCriteria(typeof(ContactType))
                    .Add(Restrictions.Eq("Username", contactTypeName))
                    .UniqueResult<ContactType>();
                return contactType;
            }

        }
    }
}
